<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\middleware\Auth;
use app\BaseController;
use app\common\model\Post\Post as PostModel;
use think\facade\View;
use think\facade\Db;

class Post extends BaseController
{
    protected $middleware = [
        Auth::class
    ];
    
    public function index()
    {
        $nav_title = [
            'nav_title' => '问答管理',
            'title' => '帖子列表'
        ];

        // 接收查询字段
        $SearchField = $this->request->param('search_field','title');

        $keyword = $this->request->param('keyword','');

        $where = [];

        if(!empty($SearchField) && !empty($keyword))
        {
            $where[] = [$SearchField,'like',"%$keyword%"];
        }else{
            $keyword = '请输入相应的内容';
        }

        $PostList = PostModel::hasWhere('user',[['deletetime','EXP',Db::raw('IS NULL')]])->with('cate')->where($where)->order('createtime','desc')->paginate(['list_rows' => 5,'query' => request()->param()]);

        // 编号 currentPage 当前页 listRows 每页要显示多少条数据
        $num = ($PostList->currentPage() - 1) * $PostList->listRows();

        return View::fetch('',[
            'nav_title' => $nav_title,
            'PostList' => $PostList,
            'num' => $num,
            'SearchField' => $SearchField,
            'keyword' => $keyword
        ]);

    }

    public function recycle()
    {
        $nav_title = [
            'nav_title' => '问答管理',
            'title' => '回收站'
        ];

        // 接收查询字段
        $SearchField = $this->request->param('search_field','title');

        $keyword = $this->request->param('keyword','');

        $where = [];

        if(!empty($SearchField) && !empty($keyword))
        {
            $where[] = [$SearchField,'like',"%$keyword%"];
        }else{
            $keyword = '请输入相应的内容';
        }

        $PostList = PostModel::onlyTrashed()->hasWhere('user',[['deletetime','EXP',Db::raw('IS NULL')]])->with('cate')->where($where)->order('createtime','desc')->paginate(['list_rows' => 5,'query' => request()->param()]);

        // 编号 currentPage 当前页 listRows 每页要显示多少条数据
        $num = ($PostList->currentPage() - 1) * $PostList->listRows();

        return View::fetch('',[
            'nav_title' => $nav_title,
            'PostList' => $PostList,
            'num' => $num,
            'SearchField' => $SearchField,
            'keyword' => $keyword
        ]);
    }


    // 恢复
    public function recovery()
    {
        if($this->request->isPost())
        {
            $postid = $this->request->param('postid');

            if(empty($postid))
            {
                $this->error('请选择帖子');
            }


            if(is_array($postid))
            {

                $PostList = PostModel::onlyTrashed()->whereIn('id',$postid)->column('id');

                $NotId = '';

                foreach($postid as $item)
                {
                    if(!in_array($item,$PostList))
                    {
                        $NotId .= $item . ',';
                    }
                }

                $NotId = rtrim($NotId,',');

                if(!empty($NotId))
                {
                    $this->error("ID：{$NotId}不存在，请重新选择");
                }

                $result = PostModel::onlyTrashed()->whereIn('id',$postid)->update(['deletetime' => null]);

                

                if($result != true)
                {
                    $this->error('批量恢复失败');
                }else{
                    $this->success('批量恢复成功');
                }

            }else{
                $post = PostModel::onlyTrashed()->find($postid);

                if(!$post)
                {
                    $this->error('要恢复的的记录不存在');
                }

                // 还原
                $result = $post->restore();

                if($result !== true)
                {
                    $this->error('恢复失败');
                }else{
                    $this->success('恢复成功');
                }
            }
        }
    }


    public function del()
    {
        if($this->request->isAjax())
        {
            $postid = $this->request->param('postid');

            $action = $this->request->param('action');

            if(empty($postid))
            {
                $this->error('请选择帖子');
            }

            if(is_array($postid))
            {
                

                if($action == 'delete')
                {
                    $PostList = PostModel::onlyTrashed()->whereIn('id',$postid)->column('id');
                }else{
                    $PostList = PostModel::whereIn('id',$postid)->column('id');

                }

                $NotId = '';

                foreach($postid as $item)
                {
                    if(!in_array($item,$PostList))
                    {
                        $NotId .= $item . ',';
                    }
                }

                $NotId = rtrim($NotId,',');

                if(!empty($NotId))
                {
                    $this->error("ID：{$NotId}不存在，请重新选择");
                }

                if($action == 'delete')
                {
                    $result = PostModel::destroy($postid,true);
                }else{
                    // 软删除
                    $result = PostModel::destroy($postid);
                }

                if($result !== true)
                {
                    $this->error('批量删除失败');
                }else{
                    $this->success('批量删除成功');
                }
                

            }else{
                // 单条删除
                if($action == 'delete')
                {
                    $post = PostModel::onlyTrashed()->find($postid);
                }else{
                    // 软删除
                    $post = PostModel::find($postid);
                }
                

                if(!$post)
                {
                    $this->error('该帖子不存在，无法删除');
                }

                if($action == 'delete')
                {
                    $result = PostModel::destroy($postid,true);
                }else{
                    // 软删除
                    $result = PostModel::destroy($postid);
                }

                if($result !== true)
                {
                    $this->error('删除失败');
                }else{
                    $this->success('删除成功');
                }

            }
        }
    }
}
